package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.service.AdminRoleService;
import com.cskaoyan.market.util.Permission;
import com.cskaoyan.market.util.PermissionUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.PermVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/admin/role")
public class AdminRoleController extends HttpServlet {

    private static final String YOUR_PACKAGE_NAME = "com.cskaoyan.market.controller.admin";

    private List<PermVo> systemPermissions;

    private Set<String> systemPermissionsString = null;


    @Autowired
    AdminRoleService service ;
    @Autowired
    private ApplicationContext context;
    @Autowired
    SecurityManager securityManager;


    @RequestMapping("options")
    public Object options() throws JsonProcessingException {
        Object o = service.options();
        return o;
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"},button = "查询")
    @GetMapping("list")
    public Object list(Integer page, Integer limit,String sort,String order){

        List<MarketRole> list =service.seletAllroles(page,limit,sort,order);
        return ResponseUtil.okList(list);

    }


    @RequiresPermissions("admin:role:permissions")
    @RequiresPermissionsDesc(menu = {"系统管理", "角色管理"},button = "授权")
    @GetMapping("permissions")
    public Object permission(Integer roleId){
        List<PermVo> systemPermissions = getSystemPermissions();
        Set<String> assignedPermissions = getAssignedPermissions(roleId);
        Boolean flag = false;
        for (String assignedPermission : assignedPermissions)
            if(assignedPermission.equals("*")) flag =true;
        if(flag)
        {
            assignedPermissions.clear();
            for (PermVo permVo : systemPermissions) {
                recursive(permVo, assignedPermissions);
            }
        }

        Map<String,Object> data = new HashMap<>();
        data.put("assignedPermissions",assignedPermissions);
        data.put("systemPermissions",systemPermissions);
        return ResponseUtil.ok(data);
    }
    private void recursive(PermVo permVo, Set<String> assignedPermissions) {
        List<PermVo> children = permVo.getChildren();
        if(children != null){
            //next level
            for (PermVo child : children) {
                recursive(child, assignedPermissions);
            }
        }else {
            assignedPermissions.add(permVo.getId());
        }
    }
    private List<PermVo> getSystemPermissions() {
        String basicPackage = YOUR_PACKAGE_NAME;
        if (systemPermissions == null) {
            List<Permission> permissions = PermissionUtil.listPermission(context, basicPackage);
            systemPermissions = PermissionUtil.listPermVo(permissions);
            systemPermissionsString = PermissionUtil.listPermissionString(permissions);
        }
        return systemPermissions;
    }
    private Set<String> getAssignedPermissions(Integer roleId) {

        Set<String>  assignedPermissions = service.selectPermission(roleId);
        return assignedPermissions;

    }


    @PostMapping("permissions")
    public Object postPerssions(@RequestBody Map map){

        Integer roleId = (Integer) map.get("roleId");
        List<String> permissions = (List<String>) map.get("permissions");

        service.insertPermissions(permissions,roleId);
        return ResponseUtil.ok();
    }

}
